﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FilesUploader.Model;
using System.Windows.Browser;

namespace FilesUploader
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Uploader = new UploaderModel();
            HtmlPage.RegisterScriptableObject("UploaderModel", Uploader);
        }

        UploaderModel Uploader;

        private void filesUpload_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "Image files (*.jpg;*.png;*.tiff;*.tif;*.CR2;*.NEF;*.RAF)|*.jpg;*.png;*.tiff;*.tif;*.CR2;*.NEF;*.RAF;*.rar;*.zip;*.7z";
            ofd.Filter = "All files (*.*)|*.*";
            ofd.Multiselect = true;
            bool? dialogResult = ofd.ShowDialog();

            if (dialogResult.HasValue && dialogResult.Value)
            {
                //string uploadAddress = "http://www.fotomario.it/DownloadUploadPhotos.aspx";
                //string uploadAddress = "http://localhost:51186/DownloadUploadPhotos.aspx";
                //string uploadAddress = "http://localhost:56444/DownloadUploadPhotos.aspx";

                //App.GetParam("us") + "\\" + App.GetParam("alb")
                HtmlDocument p = HtmlPage.Document;
                Uploader.AsyncUpload(ofd.Files, new Uri(App.UploadAddress, UriKind.Absolute), App.Folder ?? "\\", null 
                //p.DocumentUri.Scheme + "://" + p.DocumentUri.Host + AddPort(p.DocumentUri.Port)
                );
            }
        }

        private string AddPort(int port)
        {
            if (port == 80)
            {
                return "/fotomario";
            }
            return ":" + port.ToString();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = Uploader;
            mess.Text = App.GetParam("alb");
        }

    }

}
